<template>
	<mt-actionsheet :actions="actions" v-model="sheetVisible">
	</mt-actionsheet>
</template>

<script>
	export default {
		data() {
			return {
				actions: [{
					name: "拍照",
					method: this.takePhoto
				}, {
					name: "从相册中选择",
					method: this.selectPhotes
				}],
				sheetVisible: true,
				selectImg: []
				
			}
		},
		methods: {
			takePhoto(){
				const _this = this;
				var cmr = plus.camera.getCamera();
				var resList = cmr.supportedImageResolutions;
				var fmt = cmr.supportedImageFormats[0];

				var res = resList[0];
				for(var i = resList.length - 1; i >= 0; i--) {
					var item = resList[i];
					var arr = item.split('*');
					if(arr && arr.length == 2 && arr[0] == 1280 && arr[1] == 720) {
						res = item;
					}
				}

				cmr.captureImage(function(path) {
					_this.handleLocalImage(path);
				}, function(err) {

				}, {
					resolution: res,
					format: fmt
				});
			},
			selectPhotes() {
				// 图片上传多张，会发生错误
				const _this = this;
				plus.gallery.pick(function(path) {
					_this.handleLocalImage(path);

				}, function(err) {

				});
				//				, {
				//					filter: "image",
				//					multiple: true,
				//					maximum: 3,
				//					system: false,
				//					onmaxed: function() {
				//						plus.nativeUI.alert('最多只能选择3张图片');
				//					}
				//				}
			},
			handleLocalImage(path) {
				const _this = this;
				if(typeof(path) == 'string') {
					path = [path];
				} else {
					path = path.files;
				}
				for(var i = 0; i < path.length; i++) {
					var newPath = path[i];
					if(newPath.indexOf('file://') >= 0) {
						newPath = newPath.replace('file://', '');
						newPath = plus.io.convertAbsoluteFileSystem(newPath);
					}

					var dstPath = newPath.substring(0, newPath.lastIndexOf('.')) +
						'_compress' +
						newPath.substring(newPath.lastIndexOf('.'));
					plus.zip.compressImage({
						src: newPath,
						dst: dstPath,
						quality: 30,
						width: '720px',
						height: 'auto'
					}, function() {
						plus.io.resolveLocalFileSystemURL(dstPath, function(entry) {
							entry.file(function(file) {
								var reader = new plus.io.FileReader();

								//								yz.loading.show();
								reader.readAsDataURL(file);
								reader.onloadend = function(e) {
									entry.remove();
									_this.selectImg.push(e.target.result);
								};
							});
						}, function(err) {
							plus.nativeUI.alert('图片无法打开，请重新选择');
						});
					});
				}
			}
		}
	}
</script>

<style>

</style>